package cn.z2huo.knowledge.concurrency.completablefuture;

import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * <p>CompletableFuture 任务编排测试
 *
 * @author z2huo
 */
@Slf4j
class CompletableFutureTaskOrderTest {

    public static void main(String[] args) {

        CompletableFuture<Void> taskFuture = CompletableFuture.runAsync(() -> {
            log.info("task1, thread is {}", Thread.currentThread().getName());
            ThreadUtil.sleep(10, TimeUnit.SECONDS);
        });

        CompletableFuture<Void> taskFuture2 = CompletableFuture.runAsync(() -> {
            log.info("task2, thread is {}", Thread.currentThread().getName());
            ThreadUtil.sleep(10, TimeUnit.SECONDS);
        });

        CompletableFuture<Void> bothFuture = CompletableFuture.allOf(taskFuture, taskFuture2);
        bothFuture.thenRunAsync(() -> {
            log.info("task3, after task1 and task2, thread is {}", Thread.currentThread().getName());
        });

        ThreadUtil.sleep(15, TimeUnit.SECONDS);
    }

}
